package com.glodon.paas.qto.dao;

import java.util.Date;
import java.util.List;

import org.springframework.data.domain.Pageable;
import org.springframework.data.mongodb.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

import com.glodon.paas.qto.model.Task;

/**
 * @author botao.liao
 */
@Repository
public interface TaskRepository extends CrudRepository<Task, String>, TaskRepositoryCustom {

    public List<Task> findByJobServerTaskId(String jobServerTaskId);

    public List<Task> findByParentId(String id);

    /**
     * 按时间查找未完成的任务.
     * 
     * @param fromTime
     * @param toTime
     * @return
     */
    @Query(value = "{'virtualTask':false, 'status':{$in:['created', 'running']}, 'startTime':{$gte:?0, $lte:?1}}")
    @org.springframework.data.jpa.repository.Query(value = "SELECT t FROM Task t WHERE t.virtualTask=0 and t.status in ('created', 'running') and t.jobServerTaskId is not null and t.startTime between ?1 and ?2")
    public List<Task> findUncomleteTasks(Date fromTime, Date toTime, Pageable pageable);

    public List<Task> findByJobIdAndVirtualTaskIsFalseOrderByStartTimeAsc(String jobId);
}
